<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="模板名称" prop="templateName">
                <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="创建者" prop="templateManager">
                <el-select v-model="queryParams.templateManager" placeholder="请选择创建者" clearable>
                    <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="产品型号" prop="templateType">
                <el-input v-model="queryParams.templateType" placeholder="请输入产品型号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="说明" prop="templateMessage">
                <el-input v-model="queryParams.templateMessage" placeholder="请输入说明" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="创建时间" prop="recordTime">
                <el-date-picker clearable v-model="queryParams.recordTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择创建时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:template:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:template:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:template:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:template:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="模板id" align="center" prop="id" /> -->
            <el-table-column label="模板名称" align="center" prop="templateName" />
            <el-table-column label="创建者" align="center" prop="templateManager">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.lyner_emp" :value="scope.row.templateManager" />
                </template>
            </el-table-column>
            <el-table-column label="产品型号" align="center" prop="templateType" />
            <el-table-column label="说明" align="center" prop="templateMessage" />
            <el-table-column label="创建时间" align="center" prop="recordTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:template:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:template:remove']">删除</el-button>
                    <!-- <el-button size="mini" type="text" icon="el-icon-mouse" @click="getList1">测试</el-button> -->

                    <el-button size="mini" type="text" icon="el-icon-mouse" @click="handlezj(scope.row)">组件</el-button>
                    <el-button size="mini" type="text" icon="el-icon-mouse" @click="zzrkFun(scope.row)">组装入库</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改模板对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="模板名称" prop="templateName">
                    <el-input v-model="form.templateName" placeholder="请输入模板名称" />
                </el-form-item>
                <el-form-item label="创建者" prop="templateManager">
                    <el-select v-model="form.templateManager" placeholder="请选择创建者">
                        <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="产品型号" prop="templateType">
                    <el-input v-model="form.templateType" placeholder="请输入产品型号" />
                </el-form-item>
                <el-form-item label="说明" prop="templateMessage">
                    <el-input v-model="form.templateMessage" placeholder="请输入说明" />
                </el-form-item>
                <el-form-item label="创建时间" prop="recordTime">
                    <el-date-picker clearable v-model="form.recordTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 组件详情对话框 -->
        <el-dialog :title="title" :visible.sync="zj" width="1000px" append-to-body>
            <el-table v-loading="loading" :data="stockList1" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <!-- <el-table-column label="id" align="center" prop="id" /> -->
                <el-table-column label="名称" align="center" prop="stockName" />
                <el-table-column label="型号" align="center" prop="stockType" />
                <el-table-column label="封装" align="center" prop="stockPackage" />
                <el-table-column label="数量" align="center" prop="stockNum" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">

                        <el-button size="mini" type="text" icon="el-icon-mouse"
                            @click="zjupdate(scope.row)">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-mouse" @click="zjdet(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <hr>
            <p>以下组件可添加</p>
            名称：<el-input style="width:150px;margin: 30px 10px" :clearable="true" placeholder="请输入内容" v-model="input2"
                @change="searchzj"></el-input>

            型号：<el-input style="width:150px;margin: 30px 10px" :clearable="true" placeholder="请输入内容" v-model="input3"
                @change="searchzj"></el-input>

            <el-button type="primary" icon="el-icon-search" @click="searchzj">搜索</el-button>
            <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <!-- <el-table-column label="id" align="center" prop="id" /> -->
                <el-table-column label="名称" align="center" prop="stockName" />
                <el-table-column label="型号" align="center" prop="stockType" />
                <el-table-column label="封装" align="center" prop="stockPackage" />
                <el-table-column label="负责人" align="center" prop="stockManager">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.lyner_emp" :value="scope.row.stockManager" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">

                        <el-button size="mini" type="text" icon="el-icon-mouse" @click="zjadd(scope.row)">添加</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams1.pageNum"
                :limit.sync="queryParams1.pageSize" @pagination="getList1" />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitzj">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 组装入库对话框 -->
        <el-dialog :title="title" :visible.sync="zzrk" width="400px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                <el-form-item label="产品名称" prop="templateName">
                    <el-input v-model="form.templateName" placeholder="请输入产品名称" />

                </el-form-item>
                <el-form-item label="产品型号" prop="templateType">
                    <el-input v-model="form.templateType" placeholder="请输入产品型号" />
                </el-form-item>
                <el-form-item label="产品封装" prop="zzpackage">
                    <el-input v-model="zzpackage" placeholder="请输入产品封装" />
                </el-form-item>
                <el-form-item label="组件数量" prop="zznum">
                    <el-input v-model="zznum" placeholder="请输入组件数量" />
                </el-form-item>
                <el-form-item label="一级位置" prop="zzA">
                    <el-input v-model="zzA" placeholder="请输入存放一级位置" />
                </el-form-item>
                <el-form-item label="二级位置" prop="zzB">
                    <el-input v-model="zzB" placeholder="请输入存放二级位置" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="subzz">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 组件数量修改对话框 -->
        <!-- <el-dialog :title="title" :visible.sync="zjnum" width="400px" append-to-body>
          <p style="font-weight:bold;padding-bottom:20px">请填写修改后的数量</p>
          请输入数量 <input type="text" v-model="updatenum">
          <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
          </div>
      </el-dialog> -->
    </div>
</template>

<script>
import { dasbCheckAPI, asbInStkAPI, listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate, getstockAPI, addstockAPI, updatestockAPI, detstockAPI } from "@/api/system/template";
import { listStock } from "@/api/system/stock";

export default {
    name: "Template",
    dicts: ['lyner_emp'],
    data() {
        return {
            // 组件搜索名称
            input2: '',
            // 组件搜索型号
            input3: '',
            // 组件入库参数
            zznum: 1,
            zzpackage: '',
            zzA: '',
            zzB: '',
            // 组件数量添加对话框
            zjaddnum: false,
            // 组件数量修改对话框
            zjnum: false,
            // 组件具体列表
            stockList1: [],
            // 库存列表
            stockList: [],
            // 组件数量
            num: '',
            // 修改后的数量
            updatenum: '',
            // 组件窗口
            zj: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 模板表格数据
            templateList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                templateName: null,
                templateManager: null,
                templateType: null,
                templateMessage: null,
                recordTime: null
            },
            // 查询参数库存
            queryParams1: {
                pageNum: 1,
                pageSize: 10,
                stockName: null,
                stockType: null,
                stockPackage: null,
                stockManager: null,
                stockNum: null,
                stockPositionA: null,
                stockPositionB: null,
                recordTime: null,
                stockClass: null,
                stockImg: null,
                stockMsg: null
            },
            // 父id
            fid: '',
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            // 添加组件数量的参数
            cs: {},
            // 修改组件数量的参数
            upcs: {},
            // 组装入库弹框
            zzrk: false,

        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询库存列表 */
        getList1() {
            // this.loading = true;
            this.queryParams1.params = {};
            if (null != this.daterangeRecordTime && '' != this.daterangeRecordTime) {
                this.queryParams1.params["beginRecordTime"] = this.daterangeRecordTime[0];
                this.queryParams1.params["endRecordTime"] = this.daterangeRecordTime[1];
            }
            listStock(this.queryParams1).then(response => {
                // console.log(response);
                this.stockList = response.rows;
                this.total = response.total;

                // this.loading = false;
            });
        },
        /** 查询模板列表 */
        getList() {
            this.loading = true;
            listTemplate(this.queryParams).then(response => {
                this.templateList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {

            this.reset();
            this.open = false;
            this.zj = false;
            this.zzrk = false;
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                templateName: null,
                templateManager: null,
                templateType: null,
                templateMessage: null,
                recordTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加模板";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getTemplate(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改模板";
            });
        },
        // 组件按钮里的搜索
        searchzj() {
            const que = {
                pageNum: 1,
                pageSize: 10,
                stockName: this.input2,
                stockType: this.input3

            }
            listStock(que).then(response => {
                console.log(response);
                this.stockList = response.rows;
                this.total = response.total;

                // this.loading = false;
            });
        },
        // 组件按钮
        handlezj(row) {
            this.reset();
            const id = row.id || this.ids
            getTemplate(id).then(response => {
                console.log(response);
                this.form = response.data;
                this.zj = true;
                this.title = "组件详情";
                this.fid = response.data.id
                this.idd = id
                this.getList1()
                // console.log(id);
                // console.log(this.fid);
                getstockAPI(id).then(res => {
                    this.stockList1 = res.data
                    // console.log(res);
                })
            });
        },
        // 组装入库
        zzrkFun(row) {
            // this.reset();
            this.zzrk = true;
            this.title = '组装入库详情'
            const id = row.id || this.ids
            getTemplate(id).then(response => {
                // console.log(response);
                this.form = response.data;
                this.fid = response.data.id
            })


        },
        // 模板的组件列表
        getstockList() {

            getstockAPI(this.fid).then(res => {
                this.stockList1 = res.data
                // console.log(res);
            })
        },
        // 组件按钮里的添加
        zjadd(row) {
            const that = this
            this.title = '添加组件数量'
            const id = row.id
            // console.log(row);
            // console.log('stockId' + id);
            // console.log(this.fid);
            this.$prompt('请输入添加数量', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^[+]{0,1}(\d+)$/,
                inputErrorMessage: '请输入正整数'

            }).then(({ value }) => {
                // console.log(value);

                // console.log(id);
                this.cs = {
                    templateId: this.fid,
                    stockId: id,
                    num: value
                }
                // console.log(this.cs);
                addstockAPI(this.cs).then(response => {
                    // console.log(response);
                    if (response.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '添加成功 ',

                        });
                        this.getstockList()
                    }

                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });

            // console.log(cs);


        },
        // 组件按钮里的修改
        zjupdate(row) {
            const id = row.stockId
            this.$prompt('请输入修改后的数量', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^[+]{0,1}(\d+)$/,
                inputErrorMessage: '请输入正整数'

            }).then(({ value }) => {
                // console.log(value);

                // console.log(id);
                this.upcs = {

                    stockId: id,
                    num: value
                }
                console.log(this.upcs);
                updatestockAPI(this.upcs).then(response => {
                    // console.log(response);
                    if (response.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '修改成功 ',

                        });
                        this.getstockList()
                    }

                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });

            // console.log(row);
            // console.log(id);
            // console.log(this.fid);
        },
        // 组件按钮里的删除
        zjdet(row) {
            const id = row.stockId
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                // console.log(id);
                detstockAPI(id).then(res => {
                    if (res.code === 200) {
                        this.$message({

                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getstockList()
                    }
                    // console.log(res);
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 组件页面提交
        submitzj() {
            this.zj = false
            this.getList();
        },
        // 组装入库页面提交
        subzz() {

            const zz = {
                id: this.fid,
                stockNum: this.zznum,
                stockName: this.form.templateName,
                stockType: this.form.templateType,
                stockPackage: this.zzpackage,
                stockPositionA: this.zzA,
                stockPositionB: this.zzB
            }
            const hy = {
                id: this.fid,
                num: this.zznum
            }
            console.log(zz);
            // console.log(hy);
            this.zzrk = false
            dasbCheckAPI(hy).then(resp => {
                // console.log(resp);
                var cf = resp.data.map(element => {
                    // console.log(element.stockName);
                    return '组件' + element.stockName + '的数量当前为' + element.stockNum + ',组装成功后数量为' + element.assembledStockNum
                })
                // console.log(cf);
                const newDatas = []
                const h = this.$createElement
                for (const i in cf) {
                    newDatas.push(h('p', null, cf[i]))
                }
                this.$confirm('是否组装入库', {
                    title: '是否组装入库',
                    message: h('div', null, newDatas),
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                    showClose: false,
                    // center: true
                }).then(() => {
                    asbInStkAPI(zz).then(res => {
                        if (res.code === 200) {
                            this.$message({
                                type: 'success',
                                message: '组装成功!'
                            });
                        }
                        // console.log(res);
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消组装'
                    });
                });
            })

            // asbInStkAPI(zz).then(res => {
            //     console.log(res);
            // })

            this.getList();
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateTemplate(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addTemplate(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除模板编号为"' + ids + '"的数据项？').then(function () {
                return delTemplate(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/template/export', {
                ...this.queryParams
            }, `template_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
